package com.yy.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.yy.common.constant.PmsConstants;
import com.yy.dto.ComplaintsDTO;
import com.yy.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yy.common.annotation.Log;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.enums.BusinessType;
import com.yy.domain.OaComplaints;
import com.yy.service.IOaComplaintsService;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.common.core.page.TableDataInfo;

/**
 * 投诉工单Controller
 * 
 * @author yy
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/oa/complaints")
public class OaComplaintsController extends BaseController
{
    @Autowired
    private IOaComplaintsService oaComplaintsService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询投诉工单列表
     */
    @PreAuthorize("@ss.hasPermi('oa:complaints:list')")
    @GetMapping("/list")
    public TableDataInfo list(OaComplaints oaComplaints)
    {
        startPage();
        List<OaComplaints> list = oaComplaintsService.selectOaComplaintsList(oaComplaints);
        return getDataTable(list);
    }

    /**
     * 导出投诉工单列表
     */
    @PreAuthorize("@ss.hasPermi('oa:complaints:export')")
    @Log(title = "投诉工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OaComplaints oaComplaints)
    {
        List<OaComplaints> list = oaComplaintsService.selectOaComplaintsList(oaComplaints);
        ExcelUtil<OaComplaints> util = new ExcelUtil<OaComplaints>(OaComplaints.class);
        util.exportExcel(response, list, "投诉工单数据");
    }

    /**
     * 获取投诉工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('oa:complaints:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(oaComplaintsService.selectOaComplaintsById(id));
    }

    /**
     * 新增投诉工单
     */
    @PreAuthorize("@ss.hasPermi('oa:complaints:add')")
    @Log(title = "投诉工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OaComplaints oaComplaints)
    {
        //设置投诉单号
        oaComplaints.setNo(configService.selectConfigByKey(PmsConstants.OA_PREFIX_TS)+ IdUtil.getSnowflakeNextIdStr());
        //设置标题
        String title="";
        if(oaComplaints.getClient().equals(PmsConstants.OA_CLIENT_WEB_COMPANY)){
            title="【"+oaComplaints.getApplyName()+"】发起的投诉工单";
        }else{
            title="商铺【"+oaComplaints.getApplyHouse()+"】发起的投诉工单";
        }
        oaComplaints.setTitle(title);
        //设置状态
        oaComplaints.setComplaintsState(PmsConstants.OA_COMPLAINTS_STATE_APPLY);
        //设置报修时间
        oaComplaints.setApplyTime(new Date());
        //设置回访状态
        oaComplaints.setReturnState(PmsConstants.OA_RETURN_STATE_UN_VISIT);
        //设置创建者
        oaComplaints.setCreateBy(getUsername());
        return toAjax(oaComplaintsService.insertOaComplaints(oaComplaints));
    }

    /**
     * 受理工单
     */
    @PostMapping("applyComplaints")
    public AjaxResult applyComplaints(@RequestBody ComplaintsDTO complaintsDTO)
    {
        return toAjax(this.oaComplaintsService.applyComplaints(complaintsDTO));
    }

    /**
     * 处理投诉
     */
    @PostMapping("handleComplaints")
    public AjaxResult handleComplaints(@RequestBody ComplaintsDTO complaintsDTO)
    {
        return toAjax(this.oaComplaintsService.handleComplaints(complaintsDTO));
    }

    /**
     * 客户评价投诉工单
     */
    @PostMapping("doComplaintsScore")
    public AjaxResult doComplaintsScore(@RequestBody ComplaintsDTO complaintsDTO)
    {
        return toAjax(this.oaComplaintsService.doComplaintsScore(complaintsDTO));
    }

    /**
     * 提交物业回访
     */
    @PostMapping("doComplaintsVisit")
    public AjaxResult doComplaintsVisit(@RequestBody ComplaintsDTO complaintsDTO)
    {
        return toAjax(this.oaComplaintsService.doComplaintsVisit(complaintsDTO));
    }


    /**
     * 撤销工单
     */
    @PostMapping("doCancel/{id}")
    public AjaxResult doCancel(@PathVariable Long id)
    {
        return toAjax(this.oaComplaintsService.doCancel(id));
    }


}
